<?php

class Ext_Publish_Forward_Save
{
    protected $_userId;
    protected $_articleId;
    protected $_savers = array();
    protected $_messager;
    const ARTICLE_RECORD_NOT_FOUND   = 'articleRecordNotFound';
    const ACCESS_DENIED              = 'accessDenied';
    const INVALID                    = 'invalid';
    protected $_messageTemplates = array(
        self::ARTICLE_RECORD_NOT_FOUND   => "Article record not found.",
        self::ACCESS_DENIED              => "Access denied.",
        self::INVALID                    => "Invalid.",
    );
    protected $_messages = array();

    public function __construct($articleId)
    {
        $auth = Ext_Auth::getInstance();
        $_userId = $auth->getIdentity();

        $this->_userId             = $_userId;
        $this->_articleId          = $articleId;
        
        $this->_messager = new Ext_Messager($this->_messageTemplates);
    }
    
    public function addSaver(Ext_Publish_Article_Save_Interface $saver)
    {
        $this->_savers[] = $saver;
        
        return $this;
    }
    
    public function isValid()
    {
        $_articleRow = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$_articleRow){
            $this->addMessage($this->_messager->addMessage(self::ARTICLE_RECORD_NOT_FOUND));
            return false;
        }
        
        if($_articleRow['userId'] != $this->_userId){
            $this->addMessage($this->_messager->addMessage(self::ACCESS_DENIED));
            return false;
        }
        
        if(!$_articleRow['forwardArticleId']){
            $this->addMessage($this->_messager->addMessage(self::INVALID));
            return false;
        }
        
        $result = true;
        foreach ($this->_savers as $_saver) {
            if ($_saver->isValid()) {
                continue;
            }
            $result = false;
            $messages = $_saver->getMessages();
            $this->addMessages($messages);
        }
        return $result;
    }
    
    public function save()
    {    
        foreach ($this->_savers as $_saver) {
            $_saver->save();
        }
        
        $articleRow = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        $forwardArticleId = isset($articleRow['forwardArticleId']) ? $articleRow['forwardArticleId'] : '';
        $inputArray = array(
            'forwards'         => "forwards+1",
        );
        App_Model_DbTable_Article::getInstance()->addOld($inputArray, $forwardArticleId);
        
        $row = array(
            'lastForwardId'             => '',
        );
        App_Model_DbTable_User_Profile::getInstance()->addOld($row, $this->_userId);

        $_saveSearchTagser = new Ext_Article_Publish_Save_Search_Tags($this->_articleId);
        $_saveSearchTagser->save();
        
        $_saveSearchThreadser = new Ext_Article_Publish_Save_Search_Threads($this->_articleId);
        $_saveSearchThreadser->save();
    }
    
    public function addMessage($message)
    {
        $this->_messages[] = $message;
    }
    
    public function addMessages($messages)
    {
        $this->_messages = array_merge($this->_messages, $messages);
    }
    
    public function getMessages()
    {
        return $this->_messages;
    }
}
